package com.company.app.serviceImpl;

import java.util.Arrays;
import java.util.List;

import javax.jws.WebService;
import javax.xml.ws.WebServiceException;

import com.company.app.bo.ProductBO;
import com.company.app.entities.Product;
import com.company.app.entities.ProductType;
import com.company.app.service.ProductService;
import com.company.app.validators.ProductValidator;

/**
 * The Class ProductServiceImpl.
 */
@WebService(endpointInterface = "com.company.app.service.ProductService", name = "ProductService")
public class ProductServiceImpl implements ProductService {

	/** The product bo. */
	private ProductBO productBO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.ProductService#getProductById(java.lang.Long)
	 */
	@Override
	public Product getProductById(Long id) throws WebServiceException {
		return this.productBO.getProductById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.ProductService#searchProductByFilter(com.company
	 * .app.entities.Product)
	 */
	@Override
	public List<Product> searchProductByFilter(Product product)
			throws WebServiceException {
		return this.productBO.searchProducts(Arrays.asList(product));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.ProductService#executeProductBussinessLogic()
	 */
	@Override
	public boolean executeProductBussinessLogic() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.ProductService#createProduct(com.company.app.
	 * entities.Product)
	 */
	@Override
	public boolean createProduct(Product product) throws WebServiceException {
		String errorMsg = ProductValidator.isProuctValid(product);
		if (errorMsg.length() > 0) {
			throw new WebServiceException(errorMsg);
		}
		return this.productBO.createProduct(product)!=null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.ProductService#updateProduct(com.company.app.
	 * entities.Product)
	 */
	@Override
	public boolean updateProduct(Product product) throws WebServiceException {
		String errorMsg = ProductValidator.isProuctValid(product);
		if (errorMsg.length() > 0) {
			throw new WebServiceException(errorMsg);
		}
		return this.productBO.updateProduct(product);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.ProductService#deleteProduct(com.company.app.
	 * entities.Product)
	 */
	@Override
	public boolean deleteProduct(Product product) throws WebServiceException {
		return this.productBO.deleteProduct(product);
	}

	/**
	 * Sets the product bo.
	 * 
	 * @param productBO
	 *            the new product bo
	 */
	public void setProductBO(ProductBO productBO) throws WebServiceException {
		this.productBO = productBO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.service.ProductService#getAllProducts()
	 */
	@Override
	public List<Product> getAllProducts() throws WebServiceException {
		return this.productBO.getAllProduct();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.service.ProductService#createTmpProduct()
	 */
	@Override
	public Product createTmpProduct() {
		Product newTmpProduct = new Product();
		newTmpProduct.setCode("N/A");
		newTmpProduct.setName("N/A");
		newTmpProduct.setProductType(new ProductType());
		newTmpProduct.getProductType().setId(1L);
		newTmpProduct.setPrice(0.0);
		newTmpProduct.setQuantity(0);
		return this.productBO.createProduct(newTmpProduct);
	}

}
